# 文件：2_svm_iris.py
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report

# 1. 读外部数据
df = pd.read_excel('iris.xlsx')      # 若用 txt：pd.read_csv('iris.txt', sep='\t')
X = df.iloc[:, :-1].values
y = df['target'].values

# 2. 训练/测试划分
X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.3, random_state=42, stratify=y)

# 3. 建模
clf = SVC(kernel='rbf', C=1.0, gamma='scale')  # 可改 linear/poly
clf.fit(X_train, y_train)

# 4. 评估
y_pred = clf.predict(X_test)
print('Accuracy:', accuracy_score(y_test, y_pred))
print(classification_report(y_test, y_pred, target_names=['setosa','versicolor','virginica']))